<template>
    <div class="login-container">
        <div class="login-card">
            <h2 class="login-title">用户登录</h2>

            <!-- 表单错误提示 -->
            <div v-if="errorMessage" class="error-message">
                {{ errorMessage }}
            </div>

            <!-- 登录表单 -->
            <form @submit.prevent="handleLogin">
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" id="username" v-model="form.username" placeholder="请输入用户名" required />
                </div>

                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" id="password" v-model="form.password" placeholder="请输入密码" required />
                </div>

                <!-- 加载状态 -->
                <button type="submit" :disabled="isLoading" class="submit-btn">
                    {{ isLoading ? '登录中...' : '立即登录' }}
                </button>
            </form>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import http from '../utils/http' // 引入封装的 Axios 实例
import { useRouter } from 'vue-router'

const router = useRouter()

// 表单数据
const form = ref({
    username: '',
    password: ''
})

// 状态管理
const isLoading = ref(false)
const errorMessage = ref('')

// 表单验证规则
const validateForm = () => {
    if (!form.value.username.trim()) {
        errorMessage.value = '用户名不能为空'
        return false
    }

    if (!form.value.password.trim()) {
        errorMessage.value = '密码不能为空'
        return false
    }

    errorMessage.value = ''
    return true
}

// 登录处理函数
const handleLogin = async () => {
    if (!validateForm()) return

    isLoading.value = true
    try {
        const response = await http.post('/jwt/login', form.value)

        console.log("response.data")
        console.log(response.data.token)
        // 假设后端返回结构包含 token 和用户信息


        // 存储 token（建议使用更安全的存储方式）
        localStorage.setItem('token', response.data.token)
        // localStorage.setItem('user', JSON.stringify(response.resul))

        // 跳转到首页
        router.push('/HelloWorld')
    } catch (err) {
        console.error('登录失败:', err)
        errorMessage.value = err.response?.data?.message || '登录失败，请重试'
    } finally {
        isLoading.value = false
    }
}
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
    background: white;
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    width: 100%;
    max-width: 400px;
}

.login-title {
    text-align: center;
    margin-bottom: 1.5rem;
    color: #2c3e50;
    font-size: 1.5rem;
}

.form-group {
    margin-bottom: 1.25rem;
}

label {
    display: block;
    margin-bottom: 0.5rem;
    color: #666;
}

input {
    width: 100%;
    padding: 0.875rem;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 1rem;
    transition: border-color 0.3s ease;
}

input:focus {
    outline: none;
    border-color: #3498db;
}

.submit-btn {
    width: 100%;
    padding: 1rem;
    background: #3498db;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.3s ease;
}

.submit-btn:hover {
    background: #2980b9;
}

.submit-btn:disabled {
    background: #bdc3c7;
    cursor: not-allowed;
}

.error-message {
    color: #e74c3c;
    margin-bottom: 1rem;
    font-size: 0.9rem;
    text-align: center;
}
</style>